package com.example.myapplication.module1.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.example.myapplication.ConstantsData
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityWorkNoticeBinding
import com.example.myapplication.entity.BroadcastBean
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module1.adapter.BroadcastAdapter
import com.example.myapplication.module1.presenter.CompanyNoticePresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.BroadcastRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.adapter.BaseRecyclerAdapter
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.UiTools
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CompanyNoticeActivity :
    BaseMvpActivity<CompanyNoticeActivity, CompanyNoticePresenter, ActivityWorkNoticeBinding>() {
    private var itemBean: ItemBean? = null
    lateinit var loadPopup: LoadingPopupView
    private val broadcastRequest = BroadcastRequest()
    private val soapRequestBody = SoapRequestBody()
    private val soapEnvelop = SoapEnvelop()
    private var curPage = 1
    private val tempBroadcastBeans = mutableListOf<BroadcastBean>()
    val broadcastAdapter: BroadcastAdapter by lazy {
        BroadcastAdapter(this)
    }

    override fun getPresenter(): CompanyNoticePresenter {
        return CompanyNoticePresenter()
    }

    override fun getBinding(): ActivityWorkNoticeBinding {
        return ActivityWorkNoticeBinding.inflate(layoutInflater)
    }

    override fun initData() {
        itemBean = bundleExtra?.getParcelable<ItemBean>("itemInfo")
        viewBinding.clToolbar.setTitle(itemBean?.itemName)
        loadPopup = builder.asLoading("加载中...")

        getBroadcast()
    }

    /**
     * 调用接口访问公司公告接口
     */
    private fun getBroadcast() {
        if (curPage == 1) {
            tempBroadcastBeans.clear()
        }
        broadcastRequest.curpage = "$curPage"
        soapRequestBody.value = broadcastRequest
        soapEnvelop.body = soapRequestBody
        mPresenter?.getBroadcast(soapEnvelop)
    }

    override fun initView() {
        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {
            }
        }
        viewBinding.list.srlRefresh.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                curPage = 1
                getBroadcast()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                curPage += 1
                getBroadcast()
            }
        })

        viewBinding.list.rvList.adapter = broadcastAdapter

        broadcastAdapter.clickListener = object : BaseRecyclerAdapter.ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val broadcastBeans = broadcastAdapter.broadcastBeans
                if (!broadcastBeans.isNullOrEmpty()) {
                    val broadcastBean = broadcastBeans[position]
                    val bundle = Bundle()
                    bundle.putParcelable("broadcast", broadcastBean)
                    bundle.putString("title", viewBinding.clToolbar.getTitle())
                    startNextActivityForResult(
                        this@CompanyNoticeActivity,
                        ConstantsData.FINISH_ACTIVITY,
                        bundle,
                        BroadcastDetailActivity::class.java
                    )
                }
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
                return false
            }
        }
    }

    fun setBroadcast(result: String) {
        viewBinding.list.srlRefresh.setEnableLoadMore(result.isNotEmpty())
        if (result.isNotEmpty()) {
            viewBinding.list.srlRefresh.setEnableLoadMore(true)
            val broadcastBeans = GsonUtil.fromJson<MutableList<BroadcastBean>>(
                result,
                object : TypeToken<MutableList<BroadcastBean>>() {}.type
            )
            tempBroadcastBeans.addAll(broadcastBeans)
            broadcastAdapter.broadcastBeans = tempBroadcastBeans
        } else {
            UiTools.showToast(R.string.noMoreInfo)
        }
        if (tempBroadcastBeans.isEmpty()) {
            viewBinding.noData.clNoData.visibility = View.VISIBLE
        } else {
            viewBinding.noData.clNoData.visibility = View.GONE
        }
    }

    // 结束刷新状态
    fun finishRefresh() {
        if (viewBinding.list.srlRefresh.isRefreshing || viewBinding.list.srlRefresh.isLoading) {
            viewBinding.list.srlRefresh.finishRefresh()
            viewBinding.list.srlRefresh.finishLoadMore()
        } else {
            loadPopup.dismiss()
        }
    }

    // 展示加载状态
    fun showRefresh() {
        if (!viewBinding.list.srlRefresh.isRefreshing && !viewBinding.list.srlRefresh.isLoading) {
            loadPopup.show()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                ConstantsData.FINISH_ACTIVITY -> {
                    // 详情页点击了主页,当前页也要关闭
                    finish()
                }
                else -> {
                }
            }
        }
    }

}
